package o;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class bor {
    private static final bor ˏ = new bor();
    private final Context ˊ = ContentManager.getInstant().getContext();
    private final Object ˋ = new Object();
    private final SharedPreferences ˎ = this.ˊ.getSharedPreferences("com.huawei.appgallery.dynamiccore.tasks", 0);
    private final List<bol> ॱ = new LinkedList();
    private final bku ॱॱ = new bku();

    public interface b {
        void ॱ(@NonNull bol o_bol);
    }

    @NonNull
    public static bor ˎ() {
        return ˏ;
    }

    private bor() {
    }

    public void ˋ() {
        synchronized (this.ˋ) {
            for (Object next : this.ˎ.getAll().values()) {
                if (next instanceof String) {
                    bol o_bol = (bol) this.ॱॱ.ˊ(bol.class, (String) next);
                    if (o_bol != null) {
                        this.ॱ.add(o_bol);
                    }
                }
            }
        }
    }

    public void ˎ(@Nullable b bVar) {
        synchronized (this.ˋ) {
            Editor edit = this.ˎ.edit();
            Iterator it = this.ॱ.iterator();
            while (it.hasNext()) {
                bol o_bol = (bol) it.next();
                if (ˏ(o_bol)) {
                    edit.remove(String.valueOf(o_bol.ˏ()));
                    it.remove();
                    if (bVar != null) {
                        bVar.ॱ(o_bol);
                    }
                }
            }
            edit.apply();
        }
    }

    private boolean ˏ(bol o_bol) {
        if (o_bol.ˎ() != bla.ॱ(this.ˊ, o_bol.ˋ())) {
            return true;
        }
        if (o_bol.ᐝ() && bof.ˏ().ॱ(o_bol.ʽ()) == null && bof.ˋ().ˏ((long) o_bol.ˏ()) == null) {
            return true;
        }
        return false;
    }

    public void ˊ(@NonNull String str, @Nullable b bVar) {
        synchronized (this.ˋ) {
            Editor edit = this.ˎ.edit();
            Iterator it = this.ॱ.iterator();
            while (it.hasNext()) {
                bol o_bol = (bol) it.next();
                if (o_bol.ˋ().equals(str)) {
                    edit.remove(String.valueOf(o_bol.ˏ()));
                    it.remove();
                    if (bVar != null) {
                        bVar.ॱ(o_bol);
                    }
                }
            }
            edit.apply();
        }
    }

    public void ˏ(@Nullable b bVar) {
        synchronized (this.ˋ) {
            Editor edit = this.ˎ.edit();
            Iterator it = this.ॱ.iterator();
            while (it.hasNext()) {
                bol o_bol = (bol) it.next();
                it.remove();
                if (bVar != null) {
                    bVar.ॱ(o_bol);
                }
            }
            edit.clear();
            edit.apply();
        }
    }

    public void ॱ(bol o_bol) {
        if (o_bol != null) {
            synchronized (this.ˋ) {
                this.ॱ.add(o_bol);
                Editor edit = this.ˎ.edit();
                edit.putString(String.valueOf(o_bol.ˏ()), this.ॱॱ.ॱ(o_bol));
                edit.apply();
            }
        }
    }

    public void ˎ(bol o_bol) {
        synchronized (this.ˋ) {
            Editor edit = this.ˎ.edit();
            edit.putString(String.valueOf(o_bol.ˏ()), this.ॱॱ.ॱ(o_bol));
            edit.apply();
        }
    }

    public void ˊ(int i) {
        synchronized (this.ˋ) {
            Editor edit = this.ˎ.edit();
            Iterator it = this.ॱ.iterator();
            while (it.hasNext()) {
                if (((bol) it.next()).ˏ() == i) {
                    it.remove();
                    edit.remove(String.valueOf(i));
                    edit.apply();
                    return;
                }
            }
        }
    }

    @Nullable
    public bol ˋ(int i) {
        bol o_bol;
        synchronized (this.ˋ) {
            for (bol o_bol2 : this.ॱ) {
                if (o_bol2.ˏ() == i) {
                    break;
                }
            }
            o_bol2 = null;
        }
        return o_bol2;
    }

    @NonNull
    public List<bol> ˎ(String str) {
        List<bol> unmodifiableList;
        synchronized (this.ˋ) {
            List arrayList = new ArrayList();
            for (bol o_bol : this.ॱ) {
                if (o_bol.ˋ().equals(str)) {
                    arrayList.add(o_bol);
                }
            }
            unmodifiableList = Collections.unmodifiableList(arrayList);
        }
        return unmodifiableList;
    }

    @Nullable
    public bol ˊ(String str) {
        bol o_bol;
        synchronized (this.ˋ) {
            for (bol o_bol2 : this.ॱ) {
                if (o_bol2.ˊ().equals(str)) {
                    break;
                }
            }
            o_bol2 = null;
        }
        return o_bol2;
    }

    @Nullable
    public bol ˎ(long j) {
        bol o_bol;
        synchronized (this.ˋ) {
            for (bol o_bol2 : this.ॱ) {
                if (o_bol2.ʽ() == j) {
                    break;
                }
            }
            o_bol2 = null;
        }
        return o_bol2;
    }

    @Nullable
    public bol ˋ(long j) {
        bol o_bol;
        synchronized (this.ˋ) {
            for (bol o_bol2 : this.ॱ) {
                if (o_bol2.ॱॱ() == j) {
                    break;
                }
            }
            o_bol2 = null;
        }
        return o_bol2;
    }

    public void ˎ(int i) {
        synchronized (this.ˋ) {
            for (bol o_bol : this.ॱ) {
                if (!o_bol.ᐝ()) {
                    boolean z;
                    if (i == 1) {
                        z = true;
                    } else {
                        z = ˊ(o_bol);
                    }
                    if (z) {
                        if ((o_bol.ʻ() == 11 || o_bol.ʻ() == 21) && !epw.ॱ(this.ˊ, o_bol.ˋ())) {
                            ˋ(o_bol);
                        }
                        if (o_bol.ʻ() == 20 && bpo.ˊ(this.ˊ) && !bpo.ˏ(this.ˊ)) {
                            ʽ(o_bol);
                        }
                        if (o_bol.ʻ() == 30 && !epw.ॱ(this.ˊ, o_bol.ˋ())) {
                            ᐝ(o_bol);
                        }
                    }
                }
            }
        }
    }

    private static boolean ˊ(bol o_bol) {
        return System.currentTimeMillis() - o_bol.ᐝॱ() > 43200000;
    }

    private void ˋ(@NonNull bol o_bol) {
        boc.ˎ.ˋ("SessionManager", "startInstall, sessionId: " + o_bol.ˏ() + ", pkgName: " + o_bol.ˋ());
        bpc ˊॱ = o_bol.ˊॱ();
        if (ˊॱ == null) {
            boc.ˎ.ˊ("SessionManager", "Unreachable, 'session.getInstallTaskBuilder()' return null.");
            return;
        }
        ˊॱ.ॱ(bom.ॱ());
        cof ˎ = ˊॱ.ˎ();
        o_bol.ˋ(ˎ.ˎ());
        o_bol.ˎ(true);
        ˎ(o_bol);
        bof.ˋ().ˏ(ˎ);
    }

    private void ʽ(@NonNull bol o_bol) {
        boc.ˎ.ˋ("SessionManager", "startDownload, sessionId: " + o_bol.ˏ() + ", pkgName: " + o_bol.ˋ());
        bpg ˋॱ = o_bol.ˋॱ();
        if (ˋॱ == null) {
            boc.ˎ.ˊ("SessionManager", "Unreachable, 'session.getDownloadTaskBuilder()' return null.");
            return;
        }
        SessionDownloadTask ˎ = ˋॱ.ˎ();
        o_bol.ˊ(ˎ.ʾ());
        o_bol.ˎ(true);
        ˎ(o_bol);
        ˎ.ʻॱ(bks.ˊ((Integer[]) o_bol.ॱˎ().toArray(new Integer[0]), ","));
        bof.ˏ().ˊ(ˎ);
    }

    private void ᐝ(@NonNull bol o_bol) {
        boc.ˎ.ˋ("SessionManager", "startUninstall, sessionId: " + o_bol.ˏ() + ", pkgName: " + o_bol.ˋ());
        bpd ॱˊ = o_bol.ॱˊ();
        if (ॱˊ == null) {
            boc.ˎ.ˊ("SessionManager", "Unreachable, 'session.getUninstallTaskBuilder()' return null.");
            return;
        }
        ॱˊ.ˊ(bom.ॱ());
        col ˊ = ॱˊ.ˊ();
        o_bol.ˋ(ˊ.ˎ());
        o_bol.ˎ(true);
        ˎ(o_bol);
        bof.ˋ().ॱ(ˊ);
    }
}
